/*
 *
 * This source code is available under the "Simplified BSD license".
 *
 * Copyright (c) 2013, J. Kleiner
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the original author nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package Main;

import Models.SgModel;
import Widgets.PropDispatcher;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 * This class implements the controls for a simple signal generator using
 * the BREC standard board configuration.
 */
public class InstSg extends javax.swing.JPanel {
    private SgModel                mSgModel;
    private PropDispatcher         mDispatcher;  
    private String                 mApplTitleStr;
    
    /**
     * Creates new form InstSg
     */
    public InstSg() {
                                
        // Setup the application title string
        mApplTitleStr = "Sg "; 
        
        // Create a property change dispatcher
        mDispatcher = new PropDispatcher();
        
        // Create the models
        mSgModel      = new SgModel();

        // Generated components
        initComponents();
        
        // Register the  models with dispatcher
        mDispatcher.RegisterModel(mSgModel, "sg" );
        
        // Register the component tree containing all buttons with dispatcher
        mDispatcher.RegisterComponentTree(mPanelSgControls);
        
        //  Establish property change linkage with model
        mSgModel.addPropertyChangeListener(mDispatcher.GetPropListener());

        // Start the engines
        mSgModel.Start();  

        // Re-fire the models to ensure hmi components match current model
        mSgModel.FireAll();

        
        // Need a timer to extract data from engine, place in xy and repaint xy
        ActionListener taskPerformer = new ActionListener() {
    	   public void actionPerformed(ActionEvent evt) {             
               // Supply an action on swing thread for models to push updates
               mSgModel.UiUpdateRequest();
    	   } // End of timer action
        };
        Timer timer = new Timer( 30 , taskPerformer);
        timer.start();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mPanelSgControls = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        mPanelButtons = new javax.swing.JPanel();
        mPanelContro = new javax.swing.JPanel();
        panelInstBtnSgState = new Widgets.InstButton();
        panelInstBtnSgStatus = new Widgets.InstButton();
        panelInstBtnSgDevAddr = new Widgets.InstButton();
        panelInstBtnSgMode = new Widgets.InstButton();
        mPanelAuxCtl = new javax.swing.JPanel();
        panelInstBtnSgPower = new Widgets.InstButton();
        panelInstBtnSgAux0 = new Widgets.InstButton();
        panelInstBtnSgAux1 = new Widgets.InstButton();
        mPanelWheels = new javax.swing.JPanel();
        mPanelFreq = new javax.swing.JPanel();
        instNumberWheelFreq = new Widgets.InstNumberWheel();
        mPanelAtten = new javax.swing.JPanel();
        instNumberWheelAtten = new Widgets.InstNumberWheel();
        mPanelLo0 = new javax.swing.JPanel();
        instNumberWheelLo0 = new Widgets.InstNumberWheel();
        mPanelLo1 = new javax.swing.JPanel();
        instNumberWheelLo1 = new Widgets.InstNumberWheel();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        mPanelSgControls.setBorder(javax.swing.BorderFactory.createTitledBorder("Instrument Control"));
        mPanelSgControls.setLayout(new javax.swing.BoxLayout(mPanelSgControls, javax.swing.BoxLayout.X_AXIS));

        mPanelButtons.setLayout(new javax.swing.BoxLayout(mPanelButtons, javax.swing.BoxLayout.Y_AXIS));

        mPanelContro.setBorder(javax.swing.BorderFactory.createTitledBorder("Control"));
        mPanelContro.setLayout(new javax.swing.BoxLayout(mPanelContro, javax.swing.BoxLayout.Y_AXIS));

        panelInstBtnSgState.setLabel("State");
        panelInstBtnSgState.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgState.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgState.setOptions(new String[] {"ON", "OFF"});
        panelInstBtnSgState.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgState.setValue("OFF");
        panelInstBtnSgState.setValueName("sg-StateStr");
        mPanelContro.add(panelInstBtnSgState);

        panelInstBtnSgStatus.setLabel("Status");
        panelInstBtnSgStatus.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgStatus.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgStatus.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgStatus.setValue("OK");
        panelInstBtnSgStatus.setValueName("sg-StatusStr");
        mPanelContro.add(panelInstBtnSgStatus);

        panelInstBtnSgDevAddr.setDialogInput("DialogString");
        panelInstBtnSgDevAddr.setLabel("Device");
        panelInstBtnSgDevAddr.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgDevAddr.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgDevAddr.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgDevAddr.setValue("192.168.0.2:8000");
        panelInstBtnSgDevAddr.setValueName("sg-DevStr");
        mPanelContro.add(panelInstBtnSgDevAddr);

        panelInstBtnSgMode.setLabel("Mode");
        panelInstBtnSgMode.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgMode.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgMode.setOptions(new String[] {"B", "C", "B+C"});
        panelInstBtnSgMode.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgMode.setValue("NA");
        panelInstBtnSgMode.setValueName("sg-ModeStr");
        mPanelContro.add(panelInstBtnSgMode);

        mPanelButtons.add(mPanelContro);

        mPanelAuxCtl.setBorder(javax.swing.BorderFactory.createTitledBorder("Synthesizer Parameters"));
        mPanelAuxCtl.setLayout(new javax.swing.BoxLayout(mPanelAuxCtl, javax.swing.BoxLayout.Y_AXIS));

        panelInstBtnSgPower.setLabel("Main");
        panelInstBtnSgPower.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgPower.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgPower.setOptions(new String[] {"ON", "OFF"});
        panelInstBtnSgPower.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgPower.setValue("OFF");
        panelInstBtnSgPower.setValueName("sg-PowerStr");
        mPanelAuxCtl.add(panelInstBtnSgPower);

        panelInstBtnSgAux0.setLabel("Aux0");
        panelInstBtnSgAux0.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux0.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux0.setOptions(new String[] {"ON", "OFF"});
        panelInstBtnSgAux0.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux0.setValue("OFF");
        panelInstBtnSgAux0.setValueName("sg-Aux0Str");
        mPanelAuxCtl.add(panelInstBtnSgAux0);

        panelInstBtnSgAux1.setLabel("Aux1");
        panelInstBtnSgAux1.setMaximumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux1.setMinimumSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux1.setOptions(new String[] {"ON", "OFF"});
        panelInstBtnSgAux1.setPreferredSize(new java.awt.Dimension(125, 30));
        panelInstBtnSgAux1.setValue("OFF");
        panelInstBtnSgAux1.setValueName("sg-Aux1Str");
        mPanelAuxCtl.add(panelInstBtnSgAux1);

        mPanelButtons.add(mPanelAuxCtl);

        jPanel1.add(mPanelButtons);

        mPanelSgControls.add(jPanel1);

        mPanelWheels.setLayout(new javax.swing.BoxLayout(mPanelWheels, javax.swing.BoxLayout.Y_AXIS));

        mPanelFreq.setBorder(javax.swing.BorderFactory.createTitledBorder("Output(Hz)"));
        mPanelFreq.setLayout(new javax.swing.BoxLayout(mPanelFreq, javax.swing.BoxLayout.LINE_AXIS));

        instNumberWheelFreq.setBackground(new java.awt.Color(0, 0, 0));
        instNumberWheelFreq.setDigitColor(new java.awt.Color(255, 0, 0));
        instNumberWheelFreq.setValueName("sg-FreqStr");

        javax.swing.GroupLayout instNumberWheelFreqLayout = new javax.swing.GroupLayout(instNumberWheelFreq);
        instNumberWheelFreq.setLayout(instNumberWheelFreqLayout);
        instNumberWheelFreqLayout.setHorizontalGroup(
            instNumberWheelFreqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 614, Short.MAX_VALUE)
        );
        instNumberWheelFreqLayout.setVerticalGroup(
            instNumberWheelFreqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        mPanelFreq.add(instNumberWheelFreq);

        mPanelWheels.add(mPanelFreq);

        mPanelAtten.setBorder(javax.swing.BorderFactory.createTitledBorder("Offset(Hz)"));
        mPanelAtten.setLayout(new javax.swing.BoxLayout(mPanelAtten, javax.swing.BoxLayout.LINE_AXIS));

        instNumberWheelAtten.setBackground(new java.awt.Color(0, 0, 0));
        instNumberWheelAtten.setDigitColor(new java.awt.Color(255, 0, 0));
        instNumberWheelAtten.setDigits(13);
        instNumberWheelAtten.setValue("30");
        instNumberWheelAtten.setValueName("sg-AttenStr");

        javax.swing.GroupLayout instNumberWheelAttenLayout = new javax.swing.GroupLayout(instNumberWheelAtten);
        instNumberWheelAtten.setLayout(instNumberWheelAttenLayout);
        instNumberWheelAttenLayout.setHorizontalGroup(
            instNumberWheelAttenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 614, Short.MAX_VALUE)
        );
        instNumberWheelAttenLayout.setVerticalGroup(
            instNumberWheelAttenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        mPanelAtten.add(instNumberWheelAtten);

        mPanelWheels.add(mPanelAtten);

        mPanelLo0.setBorder(javax.swing.BorderFactory.createTitledBorder("First LO (Hz)"));
        mPanelLo0.setLayout(new javax.swing.BoxLayout(mPanelLo0, javax.swing.BoxLayout.LINE_AXIS));

        instNumberWheelLo0.setBackground(new java.awt.Color(0, 0, 0));
        instNumberWheelLo0.setDigitColor(new java.awt.Color(204, 204, 204));
        instNumberWheelLo0.setDigits(13);
        instNumberWheelLo0.setReadOnly(true);
        instNumberWheelLo0.setValue("30");
        instNumberWheelLo0.setValueName("sg-Lo0Str");

        javax.swing.GroupLayout instNumberWheelLo0Layout = new javax.swing.GroupLayout(instNumberWheelLo0);
        instNumberWheelLo0.setLayout(instNumberWheelLo0Layout);
        instNumberWheelLo0Layout.setHorizontalGroup(
            instNumberWheelLo0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 614, Short.MAX_VALUE)
        );
        instNumberWheelLo0Layout.setVerticalGroup(
            instNumberWheelLo0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        mPanelLo0.add(instNumberWheelLo0);

        mPanelWheels.add(mPanelLo0);

        mPanelLo1.setBorder(javax.swing.BorderFactory.createTitledBorder("Second LO (Hz)"));
        mPanelLo1.setLayout(new javax.swing.BoxLayout(mPanelLo1, javax.swing.BoxLayout.LINE_AXIS));

        instNumberWheelLo1.setBackground(new java.awt.Color(0, 0, 0));
        instNumberWheelLo1.setDigitColor(new java.awt.Color(204, 204, 204));
        instNumberWheelLo1.setDigits(13);
        instNumberWheelLo1.setReadOnly(true);
        instNumberWheelLo1.setValue("30");
        instNumberWheelLo1.setValueName("sg-Lo1Str");

        javax.swing.GroupLayout instNumberWheelLo1Layout = new javax.swing.GroupLayout(instNumberWheelLo1);
        instNumberWheelLo1.setLayout(instNumberWheelLo1Layout);
        instNumberWheelLo1Layout.setHorizontalGroup(
            instNumberWheelLo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 614, Short.MAX_VALUE)
        );
        instNumberWheelLo1Layout.setVerticalGroup(
            instNumberWheelLo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        mPanelLo1.add(instNumberWheelLo1);

        mPanelWheels.add(mPanelLo1);

        mPanelSgControls.add(mPanelWheels);

        add(mPanelSgControls);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private Widgets.InstNumberWheel instNumberWheelAtten;
    private Widgets.InstNumberWheel instNumberWheelFreq;
    private Widgets.InstNumberWheel instNumberWheelLo0;
    private Widgets.InstNumberWheel instNumberWheelLo1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mPanelAtten;
    private javax.swing.JPanel mPanelAuxCtl;
    private javax.swing.JPanel mPanelButtons;
    private javax.swing.JPanel mPanelContro;
    private javax.swing.JPanel mPanelFreq;
    private javax.swing.JPanel mPanelLo0;
    private javax.swing.JPanel mPanelLo1;
    private javax.swing.JPanel mPanelSgControls;
    private javax.swing.JPanel mPanelWheels;
    private Widgets.InstButton panelInstBtnSgAux0;
    private Widgets.InstButton panelInstBtnSgAux1;
    private Widgets.InstButton panelInstBtnSgDevAddr;
    private Widgets.InstButton panelInstBtnSgMode;
    private Widgets.InstButton panelInstBtnSgPower;
    private Widgets.InstButton panelInstBtnSgState;
    private Widgets.InstButton panelInstBtnSgStatus;
    // End of variables declaration//GEN-END:variables
}
